﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SubSonic;

namespace AppBox
{
    public partial class main : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Init

        protected void Page_Init(object sender, EventArgs e)
        {
            ExtAspNet.Menu menu = new ExtAspNet.Menu();
            btnHelp.Menus.Add(menu);

            Nii.JSON.JSONArray ja = new Nii.JSON.JSONArray(XConfigHelper.HelpList);
            foreach (Nii.JSON.JSONObject jo in ja.List)
            {
                ExtAspNet.MenuButton menuItem = new ExtAspNet.MenuButton();
                menuItem.EnablePostBack = false;
                menuItem.Text = jo.getString("Text");
                menuItem.Icon = (ExtAspNet.Icon)ExtAspNet.StringUtil.EnumFromName(typeof(ExtAspNet.Icon), jo.getString("Icon"));
                menuItem.OnClientClick = String.Format("addTab('{0}', '{1}', '{2}')", jo.getString("ID"), ResolveUrl(jo.getString("URL")), jo.getString("Text"));

                menu.Items.Add(menuItem);
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitMenu();
                LoadData();
            }
        }

        private void LoadData()
        {
            //txtUser.Text = String.Format("欢迎您：<span style=\"font-weight:bold;color:red;\">{0}</span> [{1}]", User.Identity.Name, Request.UserHostAddress);
            //txtOnlineUserCount.Text = String.Format("在线用户：{0}", "2");

            //btnSysConfig.OnClientClick = Window1.GetShowReference("~/admin/config.aspx", "系统设置");

        }


        #endregion

        #region InitMenu

        private void InitMenu()
        {
            XMenuCollection menus = new Select().From<XMenu>()
                .Where(XMenu.IsMainMenuColumn).IsEqualTo(true)
                .OrderAsc(XMenu.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<XMenuCollection>();

            List<MyMenu> newMenus = XMenuHelper.GetMyMenuCollection(menus);
            ResolveMenuTree(newMenus, 0, treeMenu.Nodes);


            #region old code
            //if (XConfigHelper.MenuType.ToLower() == "accordion")
            //{
            //    ExtAspNet.Accordion accordion = new ExtAspNet.Accordion();
            //    regionLeft.Items.Add(accordion);
            //    accordion.ShowBorder = false;
            //    accordion.ShowHeader = false;

            //    foreach (MyMenu menu in newMenus)
            //    {
            //        if (menu.ParentMenuId == 0)
            //        {
            //            ExtAspNet.AccordionPane pane = new ExtAspNet.AccordionPane();
            //            accordion.Panes.Add(pane);
            //            pane.Title = menu.Name;
            //            pane.ShowBorder = false;

            //            ExtAspNet.Tree tree = new ExtAspNet.Tree();
            //            pane.Items.Add(tree);
            //            tree.ShowBorder = false;
            //            tree.ShowHeader = false;
            //            tree.AutoScroll = true;
            //            tree.EnableArrows = true;

            //            ResolveMenuTree(newMenus, menu.Id, tree.Nodes);
            //        }
            //    }
            //}
            //else
            //{
            //    ExtAspNet.Tree tree = new ExtAspNet.Tree();
            //    regionLeft.Items.Add(tree);
            //    tree.ShowBorder = false;
            //    tree.ShowHeader = false;
            //    tree.AutoScroll = true;
            //    tree.EnableArrows = true;

            //    ResolveMenuTree(newMenus, 0, tree.Nodes);
            //} 
            #endregion
        }


        /// <summary>
        /// 生成菜单树
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentMenuId"></param>
        /// <param name="nodes"></param>
        private void ResolveMenuTree(List<MyMenu> menus, int parentMenuId, ExtAspNet.TreeNodeCollection nodes)
        {
            foreach (MyMenu menu in menus)
            {
                if (menu.ParentId == parentMenuId)
                {
                    ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
                    nodes.Add(node);

                    node.Text = menu.Name;
                    node.IconUrl = menu.ImageUrl;
                    if (!String.IsNullOrEmpty(menu.NavigateUrl))
                    {
                        node.EnablePostBack = false;
                        node.NavigateUrl = ResolveUrl(menu.NavigateUrl);
                        //node.OnClientClick = String.Format("addTab('{0}','{1}','{2}')", node.NodeID, ResolveUrl(menu.NavigateUrl), node.Text.Replace("'", ""));
                    }
                    if (menu.IsTreeLeaf)
                    {
                        node.Leaf = true;
                    }

                    ResolveMenuTree(menus, menu.Id, node.Nodes);
                }
            }
        }

        #endregion

        #region Events

        protected void btnExit_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        #endregion
    }
}
